﻿using MediatR;
using Messhall.Application.Dto;
using Messhall.Application.Dto.Users;
using Messhall.Application.Dto.Users.Requests;
using Messhall.Application.Dto.Users.Responses;
using Messhall.Application.Permission.Commands;
using Messhall.Application.Permission.Services;
using Messhall.Framework.Domain.UnitOfWork;
using Messhall.Framework.Repository.UnitOfWork;
using Messhall.HostService.Interface.Permission;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Messhall.HostService.Controllers.Permission
{
    [Route("api/[controller]")]
    [ApiController]
    public class UsersController : ControllerBase, IUsers
    {
        private readonly IMediator _mediator;

        private readonly UsersAppService _usersAppService;

        /// <summary>
        /// dapr run --dapr-http-port 60112 --app-port 60110  --app-id messhall-hostserver dotnet Messhall.HostService.dll
        /// daprd -dapr-http-port 60112 -app-port 60110  -app-id messhall-hostserver -placement-host-address localhost:6050 -components-path C:\\Users\\Administrator\\.dapr\\components
        /// </summary>
        /// <param name="mediator"></param>
        /// <param name="usersAppService"></param>
        public UsersController(IMediator mediator, UsersAppService usersAppService)
        {
            this._mediator = mediator;
            this._usersAppService = usersAppService;
        }

        /// <summary>
        /// 创建用户
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [Route("create")]
        public async Task<bool> CreateAsync([FromBody] CreateUserRequest request)
        {
            var commandResult = await _mediator.Send(new CreateUsersCommand(request));

            return commandResult;
        }

        /// <summary>
        /// 获取用户列表
        /// </summary>
        [HttpGet]
        [Route("paging")]
        public async Task<PagingResponseBase<UserPagingResponse>> PagingAsync([FromQuery] UserPagingRequest request)
        {
            return await _usersAppService.PagingAsync(request);
        }

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="request"></param>
        [HttpDelete]
        [Route("remove")]
        public async Task RemoveAsync([FromBody] RemoveUsersRequest request)
        {
            await _mediator.Publish(new RemoveUsersCommend
            {
                Request = request
            });
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut]
        [Route("update")]
        public async Task<bool> UpdateAsync([FromBody] UpdateUserRequest request)
        {
            return await _mediator.Send(new UpdateUsersCommand
            {
                Request = request
            });
        }
    }
}